/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asseco.bazi2.controllers;

import com.asseco.bazi2.models.Klient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.asseco.mio.core.webapp.controller.BaseFormController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.asseco.mio.codex.controllers.*;
import org.springframework.validation.Validator;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleTypes;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Controller;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author Bojan
 */
@Controller
@RequestMapping("/novklient*")
public class NovKlientController extends BaseFormController {

    @Autowired(required = false)
    Validator validator;

    @ModelAttribute
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    protected Klient showForm(HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        String id = request.getParameter("id");

        if (!StringUtils.isBlank(id)) {

            Klient klient = new Klient();


            Configuration c = new Configuration().configure();
            SessionFactory sf = c.buildSessionFactory();
            Session session = sf.openSession();
            session.beginTransaction();

            Connection con = session.connection();  // obtain JDBC connection from Session object

         
            CallableStatement cs = con.prepareCall("{ call baza.NOV_SERVIS.get_klient_po_id(?,?) }");

            cs.setInt(2, Integer.parseInt(id));

            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.execute();

            ResultSet resultSet = (ResultSet) cs.getObject(1);
            if (resultSet != null) {
                while (resultSet.next()) {

                    klient.setKod(resultSet.getString(1));
                    klient.setIme(resultSet.getString(2));
                    klient.setPrezime(resultSet.getString(3));
                    klient.setAdresa(resultSet.getString(4));
                    klient.setTelefon(resultSet.getString(5));
                }
                resultSet.close();
            }

            session.getTransaction().commit();
            session.close();
            sf.close();

            if (klient.getIme() != null) {
                return klient;
            } else {

                saveMessage(request, "pogreshno id za klient!");

                return new Klient();

            }


        } else {
            return new Klient();
        }
    }

    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(Klient klient, BindingResult errors, HttpServletRequest request,
            HttpServletResponse response) throws SQLException {



        if (validator != null) { // validator is null during testing
            validator.validate(klient, errors);

            if (errors.hasErrors() && request.getParameter("delete") == null) { // don't validate when deleting
                return "novklient";
            }
        }

        Configuration c = new Configuration().configure();
        SessionFactory sf = c.buildSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();

        Connection con = session.connection();  // obtain JDBC connection from Session object

        // dodavanje_klient(kkod in varchar2, kime in varchar2, kprezime in varchar2, kadresa in varchar2, ktelefon in varchar2, kgrad_id in number, kout out varchar2)

        CallableStatement cs = con.prepareCall("{ call baza.NOV_SERVIS.azuriranje_klient(?,?,?,?,?,?,?,?) }");

        cs.setInt(1, klient.getId());

         cs.setString(2, klient.getKod());

        cs.setString(3, klient.getIme());

        cs.setString(4, klient.getPrezime());

        cs.setString(5, klient.getAdresa());

        cs.setString(6, klient.getTelefon());

        cs.setString(7, "2");



        cs.registerOutParameter(8, OracleTypes.VARCHAR);
        cs.execute();
        String temp = cs.getString(8);
        session.getTransaction().commit();
        session.close();
        sf.close();
        
        
            saveMessage(request, "Uspeshno e dodaden tip na sifrarnik");
           
       
        return "redirect:mainMenu";

    }
}
